/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */



package org.myorg.ctm.etk.gui.swg.action.actioncontroller;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import org.myorg.ctm.etk.database.app.CurrentDataBaseEtk;
import org.myorg.ctm.etk.database.app.CurrentLoggedUser;
import org.myorg.ctm.etk.dtb.action.*;
import org.myorg.ctm.etk.gui.app.AppEtk;
import org.myorg.ctm.etk.gui.swg.display.frame.*;
import org.myorg.ctm.etk.gui.swg.display.create.item.anm.FrmPrintableSubNewAnimal;
import org.myorg.ctm.etk.gui.swg.display.create.item.clt.FrmPrintableSubNewClient;
import org.myorg.ctm.etk.gui.swg.display.create.user.frame.FrmPrintableSubNewUsrAbs;
import org.myorg.ctm.etk.gui.swg.display.create.user.prt.FrmPrintableSubNewUsrPrt;
import org.myorg.ctm.etk.gui.swg.display.create.user.scr.FrmPrintableSubNewUsrScr;
import org.myorg.ctm.etk.gui.swg.display.optionpane.*;
import org.myorg.shr.gui.swg.util.logging.FileHandlerShr;
import org.myorg.ctm.etk.naming.NmgFrench;
import org.myorg.shr.dtb.action.ActDtbCreateDb;
import org.myorg.shr.dtb.action.ActDtbDropDb;
import org.myorg.shr.gui.swg.display.button.BtnIcnApply;
import org.myorg.shr.gui.swg.display.button.BtnIcnCancel;
import org.myorg.shr.gui.swg.display.button.BtnIcnOk;
import org.myorg.shr.gui.swg.lang.IShrObj;

/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */
 
public class ActionController implements
        ActionListener,
        IShrObj,
        IHandlerPerspAll,
        IHandlerPerspAdministrator,
        IHandlerPerspUser
{

    // ----
    // begin: instantiate logger for this class
    final static private Logger _LOGGER_ =
            Logger.getLogger(ActionController.class.getName());

    static
    {
        ActionController._LOGGER_.addHandler(FileHandlerShr.s_getInstance());
    }

    // end: instantiate logger for this class
    // ----

    private AppEtk _app_ = null;
    



    @Override
    public boolean init() {
        return true;
    }

    @Override
    public void destroy() {
       // TODO
    }

    public ActionController(
            AppEtk app
            )
    {
        this._app_ = app;
    }

    public void userPerspectiveAdministratorHandler()
    {
        if (this._app_.getFrame() != null)
            this._app_.getFrame().userPerspectiveAdministratorHandler();
    }

    public void userPerspectivePractitionerHandler()
    {
        if (this._app_.getFrame() != null)
            this._app_.getFrame().userPerspectivePractitionerHandler();
    }

    public void userPerspectiveSecretaryHandler()
    {
        if (this._app_.getFrame() != null)
            this._app_.getFrame().userPerspectiveSecretaryHandler();
    }
    
    public void propertiesHandler()
    {
       String strMessage = "propertiesHandler()";

       strMessage += "\n\n" + "    pending ...";


       EtkOptionPaneAbs.s_showDialogInfo(this._app_.getFrame(), strMessage);

    }

    public void closeSessionHandler()
    {
       this._app_.askLogin();
    }

    public void exitHandler()
    {
        if (this._app_ != null)
            this._app_.doExit();
    }

    public void aboutHandler()
    {
        EtkOptionPaneAbs.s_showDialogAbout(this._app_.getFrame());
    }

    public void deleteUserHandler()
    {
        String strMessage = "deleteUserHandler()";

       strMessage += "\n\n" + "    pending ...";


       EtkOptionPaneAbs.s_showDialogInfo(this._app_.getFrame(), strMessage);
    }

    public void newUserHandler()
    {
        String str = OptionPaneChoiceEtk.showDialogUser(this._app_.getFrame());

        if (str == null)
         return; // aborted by user

        if (str.compareTo(NmgFrench.F_STR_NAME_PRACTITIONER) == 0)
        {
            // TODO: put in a private HashSet ...
            FrmPrintableAbs frm = new FrmPrintableSubNewUsrPrt(this);
            if (! frm.init()){
                // TODO: show error
                return;
            }
            frm.pack();
            frm.setVisible(true);
            // ending
            return;
        }

        if (str.compareTo(NmgFrench.F_STR_NAME_SECRETARY) == 0)
        {
            // TODO: put in a private HashSet ...

            FrmPrintableAbs frm = new FrmPrintableSubNewUsrScr(this);

            if (! frm.init())
            {
                // TODO: show error


                return;
            }

            frm.pack();
            frm.setVisible(true);

            // ending
            return;
        }

        else
        {
            String strError = "uncaught user, str=" + str + ", forcing an exit";
            ActionController._LOGGER_.severe(strError);
            System.exit(1);
        }


    }

    public void installDbHandler()
    {
        String strMessage = "Installing database... \n\n";

        try {
            
            ActDtbCreateDb.s_getInstance().doJob(
                    CurrentLoggedUser.s_getInstance().getUserLogin(),
                    CurrentLoggedUser.s_getInstance().getUserPassword(),
                    CurrentDataBaseEtk.getDatabase());
            strMessage += "Install OK";
        } catch (Exception ex) {
             strMessage += "An error has occured : \n";
             strMessage += ex.getMessage();
             Logger.getLogger(ActionController.class.getName()).log(
                     Level.SEVERE, null, ex);
        }
        
        EtkOptionPaneAbs.s_showDialogInfo(this._app_.getFrame(), strMessage);
    }

    public void dropDbHandler(){


        String strMessage = "Deleting database... \n\n";
        //*/ Database access is disabled
        try {
            ActDtbDropDb actDb = ActDtbDropDb.s_getInstance();
            actDb.doJob(
                CurrentLoggedUser.s_getInstance().getUserLogin(),
                CurrentLoggedUser.s_getInstance().getUserPassword(),
                CurrentDataBaseEtk.getDatabase());
            strMessage += "Deleting OK";

        } catch (Exception ex) {
            strMessage += "An error has occured : \n";
            strMessage += ex.getMessage();
            Logger.getLogger(ActionController.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        //*/
       
        EtkOptionPaneAbs.s_showDialogInfo(this._app_.getFrame(), strMessage);
    }

    public void deleteItemHandler()
    {
        String strMessage = "deleteItemHandler()";

       strMessage += "\n\n" + "    pending ...";


       EtkOptionPaneAbs.s_showDialogInfo(this._app_.getFrame(), strMessage);
    }

    public void newItemHandler()
    {
        String str = OptionPaneChoiceEtk.showDialogItem(this._app_.getFrame());

        if (str == null)
         return; // aborted by user

        if (str.compareTo(NmgFrench.F_STR_NAME_CLIENT) == 0)
        {
            // TODO: put in a private HashSet ...

            FrmPrintableAbs frm = new FrmPrintableSubNewClient(this);

            if (! frm.init())
            {
                // TODO: show error


                return;
            }

            frm.pack();
            frm.setVisible(true);

            // ending
            return;
        }

        if (str.compareTo(NmgFrench.F_STR_NAME_ANIMAL) == 0)
        {
            // TODO: put in a private HashSet ...

            FrmPrintableAbs frm = new FrmPrintableSubNewAnimal(this);

            if (! frm.init())
            {
                // TODO: show error


                return;
            }

            frm.pack();
            frm.setVisible(true);

            // ending
            return;
        }

        else
        {
            String strError = "uncaught item, str=" + str + ", forcing an exit";
            ActionController._LOGGER_.severe(strError);
            System.exit(1);
        }
    }

    public void addPrtApplyCommandHandler() {


    }

    public void addPrtCancelCommandHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addPrtOkCommandHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addScrApplyCommandHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addScrCancelCommandHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addScrOkCommandHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actionPerformed(ActionEvent e) {

        try {


        if(e.getSource() instanceof BtnIcnCancel){
            System.out.println("Cancel");

            FrmPrintableAbs frm = 
                    (FrmPrintableAbs)FrmPrintableAbs.s_getFrameOwner((JButton)e.getSource());
            if(frm == null){
                
            }
            frm.dispose();
            
            return;
        }


        if(e.getSource() instanceof BtnIcnApply){
            FrmPrintableAbs frm =
                    (FrmPrintableAbs)FrmPrintableAbs.s_getFrameOwner((JButton)e.getSource());
            if(frm == null){

            }

            if(frm instanceof FrmPrintableSubNewAnimal){
                FrmPrintableSubNewAnimal frmAnm = (FrmPrintableSubNewAnimal)frm;
                frmAnm.writeUserInputs();
                //*/
                ActDtbNewItmAnm.s_getInstance().doJob(
                        CurrentLoggedUser.s_getInstance().getUserLogin(),
                        CurrentLoggedUser.s_getInstance().getUserPassword(),
                        CurrentDataBaseEtk.getDatabase());

                 //*/
            }
            if(frm instanceof FrmPrintableSubNewClient){
                FrmPrintableSubNewClient frmClt = (FrmPrintableSubNewClient)frm;
                frmClt.writeUserInputs();
                //*/
                ActDtbNewItmClt.s_getInstance().doJob(
                        CurrentLoggedUser.s_getInstance().getUserLogin(),
                        CurrentLoggedUser.s_getInstance().getUserPassword(),
                        CurrentDataBaseEtk.getDatabase());
                //*/
                        
                
            }
            if(frm instanceof FrmPrintableSubNewUsrPrt || frm instanceof FrmPrintableSubNewUsrScr){
                FrmPrintableSubNewUsrAbs frmPrt = (FrmPrintableSubNewUsrAbs)frm;
                frmPrt.writeUserInputs();
                //*/
                ActDtbNewUsr.s_getInstance().doJob(
                        CurrentLoggedUser.s_getInstance().getUserLogin(),
                        CurrentLoggedUser.s_getInstance().getUserPassword(),
                        CurrentDataBaseEtk.getDatabase());
                //*/
            }



            return;
        }
        if(e.getSource() instanceof BtnIcnOk
                ){

            FrmPrintableAbs frm =
                    (FrmPrintableAbs)FrmPrintableAbs.s_getFrameOwner((JButton)e.getSource());
            if(frm == null){

            }

            if(frm instanceof FrmPrintableSubNewAnimal){
                FrmPrintableSubNewAnimal frmAnm = (FrmPrintableSubNewAnimal)frm;
                frmAnm.dispose();
            }
            if(frm instanceof FrmPrintableSubNewClient){
                FrmPrintableSubNewClient frmAnm = (FrmPrintableSubNewClient)frm;
                frmAnm.dispose();
            }
            if(frm instanceof FrmPrintableSubNewUsrPrt){
                FrmPrintableSubNewUsrPrt frmAnm = (FrmPrintableSubNewUsrPrt)frm;
                frmAnm.dispose();
            }
            if(frm instanceof FrmPrintableSubNewUsrScr){
                FrmPrintableSubNewUsrScr frmAnm = (FrmPrintableSubNewUsrScr)frm;
                frmAnm.dispose();
            }


            return;
        }

        } catch (Exception ex) {
                    Logger.getLogger(ActionController.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.err.println("uncaught action - " + e.getSource().toString());
        System.exit(1);
    }
}




